package com.cy.milktea.controller;

import com.cy.milktea.pojo.System.JsonResult;
import com.cy.milktea.pojo.Role;
import com.cy.milktea.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping
    public JsonResult add(@RequestBody Role role) {
        if (role == null || role.getMenuIds().size() == 0){
            return JsonResult.error("请补全参数,角色权限不能为空!");
        }
        roleService.add(role);
        return JsonResult.ok("添加成功!");
    }

    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Integer id) {
        return roleService.delete(id) == -1 ? JsonResult.error("用户中含有该角色,不可删除!") : JsonResult.ok("删除成功!");
    }

    @PutMapping
    public JsonResult update(@RequestBody Role role) {
        System.out.println(role);
        roleService.update(role);
        return JsonResult.ok("修改成功!");
    }

    @GetMapping("/all")
    public JsonResult query() {
        return JsonResult.ok("查询成功!", roleService.queryAllWithMenus(null));
    }


    @GetMapping("/{status}")
    public JsonResult query(@PathVariable Integer status){
        return JsonResult.ok("按照状态查询成功!",roleService.queryAllWithMenus(status));
    }

}







































